<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>图书借阅系统 - 图书列表</title>
    <!-- 引入 Google Fonts 以使用更现代的字体 -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        /* 全局样式 */
        body {
            font-family: 'Poppins', sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f9;
            color: #333;
            background-image: url('https://images.unsplash.com/photo-1507842217343-583bb7270b66');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            backdrop-filter: blur(5px);
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
        }

        nav {
            background-color: rgba(44, 62, 80, 0.9);
            overflow: hidden;
            position: fixed;
            top: 0;
            width: 100%;
            z-index: 1000;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            transition: background-color 0.3s ease;
        }

        nav ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
        }

        nav ul li {
            margin: 0 10px;
        }

        nav ul li a {
            display: block;
            color: white;
            text-align: center;
            padding: 16px 20px;
            text-decoration: none;
            transition: background-color 0.3s ease, transform 0.3s ease;
            font-size: 16px;
        }

        nav ul li a:hover {
            background-color: #34495e;
            transform: scale(1.05);
        }

        .container {
            padding: 50px 40px 40px 40px;
            max-width: 1200px;
            width: 90%;
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 15px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
            margin-top: 50px;
            animation: fadeIn 0.5s ease;
            transition: box-shadow 0.3s ease;
        }

        .container:hover {
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3);
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        h1 {
            font-size: 3em;
            color: #2c3e50;
            text-align: center;
            margin-top: 20px;
            margin-bottom: 35px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
            background: -webkit-linear-gradient(45deg, #2c3e50, #007BFF);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .search-container {
            margin-top: 20px;
            margin-bottom: 40px;
            display: flex;
            justify-content: center;
            gap: 10px;
        }

        #search-input {
            padding: 15px;
            width: 400px;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease, box-shadow 0.3s ease;
            background-color: rgba(255, 255, 255, 0.8);
        }

        #search-input:focus {
            border-color: #007BFF;
            outline: none;
            box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
        }

        #search-button {
            padding: 15px 30px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s ease, transform 0.3s ease;
            font-size: 16px;
            font-weight: 500;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        #search-button:hover {
            background-color: #0056b3;
            transform: scale(1.05);
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            overflow: hidden;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        table th,
        table td {
            border: none;
            padding: 15px;
            text-align: center;
            transition: background-color 0.3s ease;
        }

        table th {
            background-color: #2c3e50;
            color: white;
            font-weight: bold;
            font-size: 18px;
        }

        table tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        table tr:hover {
            background-color: #e0e0e0;
        }

        table td:nth-child(2) {
            font-weight: bold;
        }

        table td:nth-child(4) {
            color: red;
        }

        table td:nth-child(4)[data-nums="high"] {
            color: green;
        }

        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
        }

        .pagination button {
            padding: 10px 15px;
            margin: 0 5px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s ease, transform 0.3s ease;
            font-size: 16px;
            font-weight: 500;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .pagination button:hover {
            background-color: #0056b3;
            transform: scale(1.05);
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>

<body>
<nav>
    <ul>
        <li><a href="http://localhost:8888/lib/allbook">图书列表</a></li>
        <li><a href="http://localhost:8888/lib/allborrow">借阅记录</a></li>
        <li><a href="http://localhost:8888/lib/alluser">用户信息</a></li>
        <li><a href="http://localhost:8888/lib/addBook">添加图书</a></li>
        <li><a href="http://localhost:8888/lib/deleteBook">删除图书</a></li>
        <li><a href="http://localhost:8888/api/outLogin">退出</a></li>
    </ul>
</nav>
<div class="container">
    <h1>图书列表</h1>
    <!-- 搜索框和搜索按钮 -->
    <div class="search-container">
        <label for="search-input"></label><input type="text" id="search-input" placeholder="输入书名进行搜索">
        <button id="search-button">搜索</button>
    </div>
    <table id="book-table">
        <thead>
        <tr>
            <th>ID</th>
            <th>书名</th>
            <th>作者</th>
            <th>数量</th>
        </tr>
        </thead>
        <tbody>
        <!-- 图书数据将通过 jQuery 动态加载 -->
        </tbody>
    </table>
    <div class="pagination" id="pagination"></div>
</div>
<script>
    $(document).ready(function () {
        const itemsPerPage = 10;
        let currentPage = 1;
        let allBooks = [];

        // 初始加载图书列表
        loadBooks('null');

        // 图书列表搜索按钮点击事件
        $('#search-button').on('click', function () {
            let searchTerm = $('#search-input').val();
            if (searchTerm == null || searchTerm === '') {
                searchTerm = 'null';
            }
            currentPage = 1;
            loadBooks(searchTerm);
        });

        // 加载图书列表的函数
        function loadBooks(searchTerm) {
            $.ajax({
                url: `/api/books/${searchTerm}`,
                method: 'GET',
                success: function (R) {
                    if (R.hasOwnProperty('code')) {
                        return;
                    }
                    allBooks = R.data;
                    const totalPages = Math.ceil(allBooks.length / itemsPerPage);
                    showPage(currentPage);
                    setupPagination(totalPages);
                },
                error: function () {
                    alert('加载图书列表失败');
                }
            });
        }

        // 显示指定页面的图书
        function showPage(page) {
            const startIndex = (page - 1) * itemsPerPage;
            const endIndex = startIndex + itemsPerPage;
            const currentBooks = allBooks.slice(startIndex, endIndex);
            $('#book-table tbody').empty();
            if (currentBooks.length === 0) {
                alert("没有匹配到您想要的数据，请重试...");
                window.location.reload();
            } else {
                if ($('#search-input').val()!== '') {
                    alert("为您查到了" + allBooks.length + "条相似数据");
                }
            }
            currentBooks.forEach(function (book) {
                let numClass = book.nums > 10? 'high' : '';
                $('#book-table tbody').append(`
                        <tr>
                            <td>${book.bid}</td>
                            <td>${book.bookName}</td>
                            <td>${book.author}</td>
                            <td data-nums="${numClass}">${book.nums}</td>
                        </tr>
                    `);
            });
        }

        // 设置分页导航
        function setupPagination(totalPages) {
            $('#pagination').empty();
            if (totalPages > 1) {
                for (let i = 1; i <= totalPages; i++) {
                    const button = $('<button></button>').text(i);
                    if (i === currentPage) {
                        button.addClass('active');
                    }
                    button.on('click', function () {
                        currentPage = i;
                        showPage(currentPage);
                        setupPagination(totalPages);
                    });
                    $('#pagination').append(button);
                }
            }
        }
    });
</script>
</body>

</html>
